package cn.ekuma.epos.datalogic.define.dao.crm.physicalbond;

import java.util.List;

import com.openbravo.data.loader.TableDBSentenceBuilder;
import e.odbo.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.crm.physicalbond.I_PhysicalBond;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.crm.physicalbond.PhysicalBond;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.sqlbuilder.QBFBuilder;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class PhysicalBondDAO extends ModifiedLogDAO<PhysicalBond>{

    public PhysicalBondDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                    "PHYSICALBOND",
                    new Field[]{
                       new Field(I_PhysicalBond.ID,Datas.STRING, Formats.STRING),
                       new Field(I_PhysicalBond.CUSTOMER_ID,Datas.STRING, Formats.STRING,BusinessPartner.class),
                       new Field(I_PhysicalBond.CREATEDATE,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_PhysicalBond.STATE,Datas.INT, Formats.INT),
                       new Field(I_PhysicalBond.VALIDITYDATE,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       
                       new Field(I_PhysicalBond.MEMO,Datas.STRING, Formats.STRING),
                       new Field(I_PhysicalBond.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_PhysicalBond.ENABLE_TIME,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_PhysicalBond.PRODUCT_ID,Datas.STRING, Formats.STRING),
                       new Field(I_PhysicalBond.VOUCHERNUM,Datas.STRING, Formats.STRING),
                       
                       new Field(I_PhysicalBond.VALIDITY_CODE,Datas.STRING, Formats.STRING),
                       new Field(I_PhysicalBond.USEDATE,Datas.TIMESTAMP, Formats.TIMESTAMP)
                     },
                    new int[]{0}
                    );
    }

	@Override
	public void writeInsertValues(DataWrite dp, PhysicalBond obj)
			throws BasicException {
	      dp.setString(1,obj.getId());
	      dp.setString(2,obj.getCustomerId());
	      dp.setTimestamp(3,obj.getCreateDate());
	      dp.setInt(4,obj.getState());
	      dp.setTimestamp(5,obj.getValidityDate());
	      
	      dp.setString(6,obj.getMemo());
	      dp.setTimestamp(7,obj.getLastModified());
	      dp.setTimestamp(8,obj.getEnableTime());
	      dp.setString(9,obj.getProductId());
	      dp.setString(10, obj.getVoucherNum());
	      
	      dp.setString(11, obj.getValidityCode());
	      dp.setTimestamp(12,obj.getUseDate());
	     
	}

	@Override
	public Class getSuportClass() {
		return PhysicalBond.class;
	}

	@Override
	public PhysicalBond readValues(DataRead dr,
			PhysicalBond obj) throws BasicException {
		if(obj==null)
			obj=new PhysicalBond();
		obj.setId(dr.getString(1));
		obj.setCustomerId(dr.getString(2));
		obj.setCreateDate(dr.getTimestamp(3));
		obj.setState(dr.getInt(4));
		obj.setValidityDate(dr.getTimestamp(5));
		
		obj.setMemo(dr.getString(6));
		obj.setLastModified(dr.getTimestamp(7));
		obj.setEnableTime(dr.getTimestamp(8));
		obj.setProductId(dr.getString(9));
		obj.setVoucherNum(dr.getString(10));
		
		obj.setValidityCode(dr.getString(11));
		obj.setUseDate(dr.getTimestamp(12));
		return obj;
	}

	public List<PhysicalBond> list(Object filter) throws BasicException {
		return new StaticSentence(s, new QBFBuilder(
                ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE ?(QBF_FILTER) "
                , new String[]{I_PhysicalBond.CREATEDATE, I_PhysicalBond.CREATEDATE, I_PhysicalBond.CUSTOMER_ID}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING}), this).list(filter);
	}
	
	public PhysicalBond findPhysicalBondByCard(String cardNum) throws BasicException {
		 return (PhysicalBond) new PreparedSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE VOUCHERNUM = ? ", SerializerWriteString.INSTANCE, this).find(cardNum);
	}
}
